package servletTestTwo.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import servletTestTwo.dao.DaoServletOne;
import servletTestTwo.entity.Entity;

public class DaoServletImpl implements DaoServletOne {

	@Override
	public boolean add(Entity userName) {
		Connection conn = DOBC.getConn();
		String sql = "insert into servlet(user_Name,phone,pass_word,delete_logo)values(?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,userName.getUserName() );
			ps.setString(2, userName.getPassWord());
			ps.setString(3,userName.getPhone());
			ps.setString(4,userName.getDeleteLogo()==null?"N":userName.getDeleteLogo());
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}

	@Override
	public boolean delete(Long id) {
		Connection conn = DOBC.getConn();
		String sql = "update servlet set delete_logo=? where id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,"Y");
			ps.setLong(2,id);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}
	//ctrl +alt +h
	@Override
	public boolean update(Entity userName) {
		Connection conn=DOBC.getConn();
//		String sql="update servlet set id=? where user_name=?";
		String sql="update servlet set user_name=? where id=?";
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, userName.getUserName());
			ps.setLong(2, userName.getId());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}

	@Override
	public List<Entity> listAll() {
		String sql = "Select * from servlet";
		Connection conn = DOBC.getConn();
		List<Entity> list = new ArrayList();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			// rs结果集
			while (rs.next()) {
				int i=1;
				Entity entity = new Entity();
				entity.setId(rs.getLong(i++));
				entity.setUserName(rs.getString(i++));
				entity.setPassWord(rs.getString(i++));
				entity.setPhone(rs.getString(i++));
				entity.setCreationTime(rs.getDate(i++));
				entity.setUpdateTime(rs.getDate(i++));
				entity.setDeleteLogo(rs.getString(i++));
				// 循环添加到List
				list.add(entity);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(rs, ps, conn);
		return list;
	}

	@Override
	public Entity getById(Long id) {
		String sql = "select id,user_name,phone,pass_word,creation_time,update_time,delete_logo from servlet where id=?";
		Connection conn = DOBC.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			rs = ps.executeQuery();
			// rs结果集
			if (rs.next()) {
				int i=1;
				Entity entity = new Entity();
				entity.setId(rs.getLong(i++));
				entity.setUserName(rs.getString(i++));
				entity.setPassWord(rs.getString(i++));
				entity.setPhone(rs.getString(i++));
				entity.setCreationTime(rs.getDate(i++));
				entity.setUpdateTime(rs.getDate(i++));
				entity.setDeleteLogo(rs.getString(i++));
				// 循环添加到List
				return entity;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(rs, ps, conn);
		return null;
	}

}
